﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yc.Dal;

namespace yc.Site.Controllers
{
	public class IntroduceController : Controller
	{
		public ActionResult Index()
		{
			var db = new DBContext();
			var item = db.SysIntroduce.ToPager("CreateDate DESC");
			return View(item);
		}

		public ActionResult Edit(int Id = 0)
		{
			var db = new DBContext();
			var item = new SysIntroduce { InterviewDate = DateTime.Now };
			if (Id != 0) item = db.SysIntroduce.Find(Id);
			var client = db.SysClient.FirstOrDefault(p => p.SysNo.Equals(item.ClientSysNo));
			var company = db.SysCompany.FirstOrDefault(p => p.SysNo.Equals(item.CompanySysNo));

			ViewBag.CompanyEmployeeSysNo = Common.UnSelect;
			ViewBag.ClientSex = new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", client != null ? client.Gender : null);
			ViewBag.ClientEducation = new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Education")).OrderBy(p => p.Sort), "Worth", "Caption", client != null ? client.Education : null);
			ViewBag.ClientWorkExpress = new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WorkExpress")).OrderBy(p => p.Sort), "Worth", "Caption", client != null ? client.WorkExpress : null);

			if (company != null) ViewBag.CompanyEmployeeSysNo = new SelectList(company.SysCompanyEmployee, "ID", "Caption", item.CompanyEmployeeSysNo);
			return View(item);
		}

		[HttpPost, ActionName("Edit")]
		public ActionResult PostEdit(int Id = 0)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysIntroduce { CreateDate = DateTime.Now };
			if (Id != 0) item = db.SysIntroduce.Find(Id);
			if (TryUpdateModel(item))
			{
				if (Id == 0) db.SysIntroduce.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
			}

			var client = db.SysClient.FirstOrDefault(p => p.SysNo.Equals(item.ClientSysNo));
			var company = db.SysCompany.FirstOrDefault(p => p.SysNo.Equals(item.CompanySysNo));

			ViewBag.CompanyEmployeeSysNo = Common.UnSelect;
			ViewBag.ClientSex = new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", client != null ? client.Gender : null);
			ViewBag.ClientEducation = new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Education")).OrderBy(p => p.Sort), "Worth", "Caption", client != null ? client.Education : null);
			ViewBag.ClientWorkExpress = new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WorkExpress")).OrderBy(p => p.Sort), "Worth", "Caption", client != null ? client.WorkExpress : null);

			if (company != null) ViewBag.CompanyEmployeeSysNo = new SelectList(company.SysCompanyEmployee, "ID", "Caption", item.CompanyEmployeeSysNo);

			return View(item);
		}

		public ActionResult Print(int Id = 0)
		{
			var db = new DBContext();
			var item = new SysIntroduce();
			if (Id != 0) item = db.SysIntroduce.Find(Id);
			var employee = db.SysCompanyEmployee.FirstOrDefault(p => p.ID.Equals(item.CompanyEmployeeSysNo ?? 0));
			var education = Common.Dictionary.Find(p => p.Category.Equals("Education") && p.Worth.Equals(item.ClientEducation));
			var gender = Common.Dictionary.Find(p => p.Category.Equals("Gender") && p.Worth.Equals(item.ClientSex));
			ViewBag.CompanyEmployeeSysNo = employee == null ? "" : employee.Caption;
			ViewBag.ClientEducation = education == null ? "" : education.Caption;
			ViewBag.ClientSex = gender == null ? "" : gender.Caption;
			return View(item);
		}

		public ActionResult LoadClient(string cid)
		{
			var db = new DBContext();
			var item = db.SysClient.FirstOrDefault(p => p.SysNo.Equals(cid)) ?? new SysClient { SysNo = cid };
			return View(item);
		}

		public ActionResult LoadCompany(string cid)
		{
			var db = new DBContext();
			var item = db.SysCompany.FirstOrDefault(p => p.SysNo.Equals(cid)) ?? new SysCompany { SysNo = cid };
			return View(item);
		}
	}
}
